package com.jiepos.mpos.biz.analysis.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppCustomerInfoDao;
import com.jiepos.mpos.common.entity.ChartEntity;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.StringUtils;


/**
 *
 *
 * <p>标题: —— 会员统计分析类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.analysis.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年10月8号
 * @history（历次修订内容、修订人、修订时间等）
 */

@Service
public class CustomAnalysisService extends BaseService {

    @Autowired
    private AppCustomerInfoDao appCustomerInfoDao;

    private String[] colors = {"AFD8F8", "F6BD0F", "8BBA00", "FF8E46", "008E8E",
            "D64646", "8E468E", "588526", "B3AA00", "008ED6", "9D080D", "A186BE"};

    /**
     * 查询日注册会员数
     * @return
     */
    public List getCustomList(HttpServletRequest request, String loc_dt_start, String loc_dt_end, String ins_cd, String ins_cn_nm ) {
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT count(t1.rec_crt_ts) as sumCustom, TO_CHAR(t1.rec_crt_ts, 'YYYY-MM-DD') AS TS ")
                .append("   FROM "+ DBConstant.Table.T_APP_CUSTOM_INF+" t1 ")
                .append(" left join "+ DBConstant.Table.T_APP_INS_INF+" t2 ")
                .append(" on t1.ins_uuid = t2.ins_uuid  ")
                .append(" where 1 = 1 ");
               List param = new ArrayList();
                //机构号
                if (!StringUtils.isBlank(ins_cd)) {
                    sqlBuf.append( " and t2.ins_cd like ? " );
                    param.add("%" + ins_cd + "%");
                }
                //机构名称
                if (!StringUtils.isBlank(ins_cn_nm)) {
                    sqlBuf.append( " and t2.ins_cn_nm like ? " );
                    param.add("%" + ins_cn_nm + "%");
                }
                //起止日期
                if (!StringUtils.isBlank(loc_dt_start)) {
                    Date dateParam = DateUtil.strToDate(loc_dt_start, DateUtil.FORMAT_DATE_1);
                    sqlBuf.append("and TO_CHAR(t1.rec_crt_ts,'yyyy-MM-dd') >= ? ");
                    param.add(DateUtil.dateToStr(dateParam, "yyyy-MM-dd"));
                }
                if (!StringUtils.isBlank(loc_dt_end)) {
                    Date dateParam = DateUtil.strToDate(loc_dt_end, DateUtil.FORMAT_DATE_1);
                    sqlBuf.append("and TO_CHAR(t1.rec_crt_ts,'yyyy-MM-dd') <= ? ");
                    param.add(DateUtil.dateToStr(dateParam, "yyyy-MM-dd"));
                }
        String ins_phone = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_PHONE));
        sqlBuf.append(" and  t1.custom_uuid in ");
                sqlBuf.append(" (select tt.custom_uuid from "+ DBConstant.Table.T_APP_CUSTOM_INF+" tt ");
                sqlBuf.append(" start with tt.login_id='" + ins_phone + "'");
                sqlBuf.append(" connect by prior tt.custom_uuid = tt.sup_custom_uuid)");
                sqlBuf.append("  group by TO_CHAR(t1.rec_crt_ts, 'YYYY-MM-DD') ")
                    .append("  ORDER BY ts ASC ");
        List<Map<String, Object>> list = appCustomerInfoDao.queryForList(sqlBuf.toString(), param);
        if(CollectionUtil.isEmpty(list)){
            return list;
        }
        return list;
    }
    /**
     * 单个图标系列
     */
    public String createRoutineChartXmlData(HttpServletRequest reques,String loc_dt_start,String loc_dt_end,String ins_cd,String ins_cn_nm) throws Exception {
        ChartEntity chartEntity = new ChartEntity();
        chartEntity.setCaption("会员日注册数统计");
        chartEntity.setxAxisName("日期("+loc_dt_start+"~"+loc_dt_end+")");
        chartEntity.setyAxisName("用户数");
        chartEntity.setShowValues("0");
        chartEntity.setDecimalPrecision("0");
        chartEntity.setFormatNumberScale("0");
        StringBuffer strXML = new StringBuffer();
        strXML.append("<graph  useRoundEdges='1'  caption='" + chartEntity.getCaption() + "' xAxisName='"
                + chartEntity.getxAxisName() + "' yAxisName='" + chartEntity.getyAxisName() + "' FormatNumberScale='"+chartEntity.getFormatNumberScale() + "' DecimalPrecision= '"
                + chartEntity.getDecimalPrecision() +"'  "
                + " yAxisMinValue = '" + "" + "' yAxisMaxValue = '"+ "" + "'>");
        List<Map<String, Object>> dataList  = this.getCustomList(reques,loc_dt_start,loc_dt_end,ins_cd,ins_cn_nm);
        for (int i = 0; i < dataList.size(); i++) {
            Map row = dataList.get(i);
            String value  = String.valueOf(row.get("sumCustom"));
            String name  = String.valueOf(row.get("ts"));
            strXML.append("<set label='" + name +"' " + "value='" + value +"'>");
            strXML.append("</set>");
        }
        strXML.append("</graph>");
        String str =  strXML.toString();
      return str;
    }
}
